
# -*- coding: UTF-8 -*-

import re
import os, sys
import hashlib

home_path = sys.argv[1]

list_path = home_path + '/parm.txt'
database_path = home_path + '/database.bin'
modify_flag = False

if not os.path.exists(list_path):
    print('error: not find %s' % list_path)
    sys.exit(1)

f_list = open(list_path, 'rb')
md5obj = hashlib.md5()
md5obj.update(f_list.read())
f_list.close()

if os.path.exists(database_path):
    f_db = open(database_path, 'rb')
    database = f_db.read()

    if bytes.decode(database) != md5obj.hexdigest():
        modify_flag = True
else:
    modify_flag = True

if modify_flag:
    f_db = open(database_path, 'wb')
    f_db.write(str.encode(md5obj.hexdigest()))
    f_db.close()
else:
    sys.exit(0)

f = open(list_path, 'r', encoding='utf8')

file_rows = 0
system_list = []
app_list = []
update_list = []

data_list = system_list

while True:
    line_data = f.readline() # 每行结尾用换行符分割
    file_rows += 1

    if not line_data: # 结尾行跳出
        break
    else:
        notes_index = line_data.find('#') # 查找注释
        
        if notes_index != -1: # 去除注释
            notes = line_data.split('-*-')
            
            if notes_index == 0 and len(notes) == 3:
                if notes[1].find('system id') != -1:
                    data_list = system_list
                elif notes[1].find('app id') != -1:
                    data_list = app_list
                elif notes[1].find('update id') != -1:
                    data_list = update_list

                continue
            else:
                line_data = line_data[0:notes_index]

        line_data = line_data.split(',')

        if len(line_data) != 2:
            if (len(line_data) == 1) and (line_data[0].strip() == ''):
                pass
            else:
                print('error: line %d' % (file_rows))
                sys.exit(1)
        else:
            title = re.findall('[a-zA-Z0-9_]+', line_data[0])

            if len(title) != 1:
                print('error: line %d' % (file_rows))
                sys.exit(1)
            else:
                title = title[0]

            content = re.findall('[a-zA-Z0-9_]+', line_data[1])

            if len(content) != 1:
                print('error: line %d' % (file_rows))
                sys.exit(1)
            else:
                content = int(content[0])

            data_list.append((title, content))

f.close()

f = open(home_path + '/parm_id.h', 'w')

# write system id
f.write('enum {\n')
f.write('    %s = 0,\n' % system_list[0][0])

for data in system_list[1:]:
    f.write('    %s,\n' % (data[0]))

f.write('    PP_SYSTEM_ID_MAX,\n};\n\n')

# write app id
f.write('enum {\n')
f.write('    %s = PP_SYSTEM_ID_MAX,\n' % app_list[0][0])

for data in app_list[1:]:
    f.write('    %s,\n' % (data[0]))

f.write('    PP_APP_ID_MAX,\n};\n\n')

# write update id
f.write('enum {\n')
f.write('    %s = PP_APP_ID_MAX,\n' % update_list[0][0])

for data in update_list[1:]:
    f.write('    %s,\n' % (data[0]))

f.write('    PP_UPDATE_ID_MAX,\n};\n')

f.close()

f = open(home_path + '/parm_map.h', 'w')

offset_addr = 0

# write system info
f.write('static const parm_info_t parm_info_map[] = {\n')

for data in system_list:
    f.write('    {%d, %d},\n' % (data[1], offset_addr))
    offset_addr += data[1] // 4 * 4
    if data[1] % 4:
        offset_addr += 4

system_parm_size = offset_addr
offset_addr = 0

# write app info
for data in app_list:
    f.write('    {%d, %d},\n' % (data[1], offset_addr))
    offset_addr += data[1] // 4 * 4
    if data[1] % 4:
        offset_addr += 4

app_parm_size = offset_addr
offset_addr = 0

# write update info
for data in update_list:
    f.write('    {%d, %d},\n' % (data[1], offset_addr))
    offset_addr += data[1] // 4 * 4
    if data[1] % 4:
        offset_addr += 4

update_parm_size = offset_addr

f.write('};\n\n')

f.write('#define SYSTEM_REGION_SIZE (%d)\n' % system_parm_size)
f.write('#define APP_REGION_SIZE (%d)\n' % app_parm_size)
f.write('#define UPDATE_REGION_SIZE (%d)\n' % update_parm_size)

f.close()
